<template>
  <div class="page-content">
    <el-card shadow="never" v-if="detail">
      <el-form ref="form" label-width="9em">
        <el-row :gutter="24">
          <el-col :span="24">
            <h3>订单信息</h3>
          </el-col>
          <el-col :span="6">
            <el-form-item label="采购单号:">{{detail.purchaseNo}}</el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="订单状态:">{{orderStateMap[detail.orderState] || '未知'}}</el-form-item>
          </el-col>
          <el-col :span="10">
            <el-form-item label="下单时间:">{{$util.formatDate(detail.orderTime)}}</el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="付款时间:">{{detail.payTime ? $util.formatDate(detail.payTime) : '-'}}</el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="供应商店铺:">{{detail.shopName}}</el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="支付方式:">{{payTypeMap[detail.payType]}}</el-form-item>
          </el-col>

          <el-col :span="24">
            <el-divider></el-divider>
          </el-col>

          <el-col :span="24">
            <h3>关联销售单</h3>
          </el-col>
          <el-col :span="24">
            <el-form-item label="关联订单号:">{{detail.relateOrderIds || '未关联'}}</el-form-item>
          </el-col>

          <!-- 收货人信息 -->
          <el-col :span="24">
            <h3>收货人信息</h3>
          </el-col>
          <el-col :span="6">
            <el-form-item label="收货人:">{{detail.consigneeName}}</el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="收货人电话:">{{detail.consigneeMobile}}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="收货地址:">{{detail.consigneeFullAddress}}</el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="订单备注:">{{detail.remark || '-'}}</el-form-item>
          </el-col>

          <!-- 商品信息 -->
          <el-col :span="24">
            <h3 style="margin-bottom: 20px">商品信息</h3>
          </el-col>
          <el-col :span="24">
            <el-table class="table" :data="detail.orderItems" border>
              <el-table-column label="商品编号" align="center" prop="skuId"></el-table-column>
              <el-table-column align="center" label="商品图片">
                <template slot-scope="{ row }">
                  <el-image
                    v-if="row.picUrl"
                    class="item-img"
                    :src="row.picUrl"
                    :preview-src-list="[row.picUrl]"
                  ></el-image>
                </template>
              </el-table-column>
              <el-table-column label="商品名称" align="center" prop="name"></el-table-column>
              <el-table-column align="center" label="商品类目">
                <template slot-scope="{ row }">{{row.categoryNames ? row.categoryNames.join(',') : '-'}}</template>
              </el-table-column>
              <el-table-column align="center" label="品牌" prop="brand"></el-table-column>
              <el-table-column align="center" label="规格型号" prop="attribute"></el-table-column>
              <el-table-column align="center" label="数量" prop="num"></el-table-column>
              <el-table-column align="right" label="单价（元）" prop="price">
                <template slot-scope="{ row }">
                  {{row.price | formatAmount}}
                </template>
              </el-table-column>
              <el-table-column align="right" label="小计（元）">
                <template slot-scope="{ row }">
                  <span class="red">{{row.totalPrice | formatAmount}}</span>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
          <el-col :span="24" class="footer">
            <div>商品总金额:<span>{{detail.totalPrice | formatAmount}}</span> 元</div>
            <div v-if="detail.discount">-优惠金额:<span>{{detail.discount | formatAmount}}</span> 元</div>
            <div v-if="detail.freight">+运费金额:<span>{{detail.freight | formatAmount}}</span> 元</div>
            <div class="price">
              应付金额:
              <span class="red">{{detail.paymentPrice | formatAmount}}</span> 元
            </div>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { detail } from '@/api/purchase/onlineOrder';
import {
  orderStateMap,
  payTypeMap,
} from '@/config/purchase/online';

export default {
  name: 'detail',

  data () {
    return {
      detail: null,
      payTypeMap,
      orderStateMap,
    };
  },

  created () {
    this.getDetail();
  },

  methods: {
    async getDetail () {
      const loading = this.$loading();
      try {
        this.detail = await detail(this.$route.params.id);
      } catch (error) {
        this.$router.back();
      }
      loading.close();
    },
  },
};
</script>

<style scoped lang="scss">
.page-content {
  max-width: 1400px;
  margin: 0 auto;
}

h3 {
  margin: 12px 0;
  font-size: 16px;
}

.el-divider {
  margin: 12px 0;
}

.el-form-item {
  margin-bottom: 0;
}

.red {
  color: red;
}

.item-img {
  width: 50px;
  max-height: 50px;
}

.footer {
  text-align: right;
  margin: 30px 0;
  color: #666;
  span {
    display: inline-block;
    min-width: 100px;
  }
}

.price {
  margin-top: 15px;
  font-size: 16px;
  color: #333;
  font-weight: bold;
}

</style>
